﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Reflection;
using System.Text;

namespace EBA.IoC
{
    public class AutowiredMember
    {
     
        public AutowiredMember(MemberInfo member,ImportAttribute import)
        {
            this.Member = member;
            this.ContractName = import.ContractName;
            if (import.ContractType == null)
            {
                if(this.Member is PropertyInfo)
                {
                    this.ContractType = ((PropertyInfo)this.Member).PropertyType;
                }
                else{
                    if (this.Member is FieldInfo)
                    {
                        this.ContractType = ((FieldInfo)this.Member).FieldType;
                    }
                    else
                    {
                        throw new NotSupportedException("The " + this.Member.MemberType + " does not support to be injected.");
                    }
                }
            }
            else
            {
                this.ContractType = import.ContractType;
            }

            this.IsCollection = false;
        }

        
        public AutowiredMember(MemberInfo member, ImportManyAttribute importMany)
        {
            this.Member = member;

            this.ContractName = importMany.ContractName;
            if (importMany.ContractType == null)
            {
                this.ContractType = this.Member.ReflectedType.GetElementType();
            }
            else
            {
                this.ContractType = importMany.ContractType;
            }

            this.IsCollection = true;
        }

        public MemberInfo Member { get; internal set; }
        public Type ContractType { get; internal set; }
        public string ContractName { get; internal set; }
        public bool IsCollection { get; internal set; }
    }
}
